package com.example.admin_oa.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.admin_oa.model.mapper.SysRolePermissionMapper;
import com.example.admin_oa.model.service.SysRoleService;
import com.example.admin_oa.model.mapper.SysRoleMapper;
import com.example.comment_oa.pojos.SysRole;
import com.example.comment_oa.pojos.SysRolePermission;
import com.example.comment_oa.pojos.SysUser;
import com.example.comment_oa.pojos.SysUserRole;
import com.example.comment_oa.pojos_vo.SysRoleVo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* @author cjl
* @description 针对表【sys_role】的数据库操作Service实现
* @createDate 2022-10-17 16:09:49
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    SysRolePermissionMapper sysRolePermissionMapper;
    @Override
    public int add(SysRoleVo sysRoleVo) {
        SysRole sysRole=new SysRole();
        sysRole.setRoleCore(sysRoleVo.getRoleCode());
        sysRole.setRoleName(sysRoleVo.getRoleName());
        sysRole.setRoleState(0);
        sysRole.setRoleOrder(2);
        sysRoleMapper.insert(sysRole);
        QueryWrapper<SysRole> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("role_id");
        wrapper.last("limit 1");
        List<SysRolePermission> sysUserRoleList=new ArrayList<>();
        SysRole sysRole1 = sysRoleMapper.selectOne(wrapper);//取出最新的一条
        System.out.println(sysRoleVo.getPerList().length);
        if(sysRoleVo.getPerList().length>0){
            System.out.println("11111111");
            for (int i = 0; i < sysRoleVo.getPerList().length; i++) {
                SysRolePermission sysRolePermission=new SysRolePermission(sysRole1.getRoleId(),Integer.parseInt(sysRoleVo.getPerList()[i]));
                sysUserRoleList.add(sysRolePermission);
            }
            return sysRolePermissionMapper.insertBatch(sysUserRoleList);
        }else{
            return 0;
        }

    }
}




